﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using Javavaj.iHunter;
using Javavaj.iHunter.Database;


namespace Javavaj.iHunter.Core
{
    public struct ChannelSettingItem :IEquatable<ChannelSettingItem>
    {
        public DateTime LastUpdateTime { get; set; }



        #region IEquatable<ChannelSettingItem> 成员

        public bool Equals(ChannelSettingItem other)
        {
            return (LastUpdateTime.Equals(other.LastUpdateTime));
        }

        #endregion

    };

    public partial class ChannelSettings : IDictionary<int, ChannelSettingItem>
    {
        public ChannelSettings(UserDbDataSet dataSet)
        {
            this.dataSet = dataSet;
        }
        UserDbDataSet dataSet;

        #region IDictionary<int,string> 成员

        public void Add(int key, ChannelSettingItem value)
        {
            if (ContainsKey(key))
                throw new InvalidOperationException();
            var row = dataSet.ChannelSettings.NewChannelSettingsRow();
            row.ChannelHash = key;
            row.LastUpdateTime = value.LastUpdateTime;
            dataSet.ChannelSettings.AddChannelSettingsRow(row);
            row.AcceptChanges();

        }

        public bool ContainsKey(int key)
        {
            var result = from row in dataSet.ChannelSettings
                         where row.ChannelHash == key
                         select row;
            var rows = result.ToArray();
            if (rows.Length >= 2)
            {
                foreach (var row in result)
                    row.Delete();
                dataSet.ChannelSettings.AcceptChanges();
                return false;
            }
            else if (rows.Length == 0)
                return false;
            else
                return true;
        }

        public ICollection<int> Keys
        {
            get
            {
                var result = from row in dataSet.ChannelSettings
                             select row.ChannelHash;
                return result.ToArray();

            }
        }

        public bool Remove(int key)
        {
            var result = from row in dataSet.ChannelSettings
                         where row.ChannelHash == key
                         select row;
            foreach (var row in result)
                row.Delete();
            dataSet.ChannelSettings.AcceptChanges();
            return result.Count() != 0;
        }

        public bool TryGetValue(int key, out ChannelSettingItem value)
        {
            var result = from row in dataSet.ChannelSettings
                         where row.ChannelHash == key
                         select row;
            var rows = result.ToArray();
            if (rows.Length >= 2)
            {
                foreach (var row in result)
                    row.Delete();
                dataSet.ChannelSettings.AcceptChanges();
                value = new ChannelSettingItem()
                    {
                        LastUpdateTime = DateTime.MinValue
                    };
                return false;
            }
            else if (rows.Length == 0)
            {
                value = new ChannelSettingItem()
                {
                    LastUpdateTime = DateTime.MinValue
                };
                return false;
            }
            else
            {
                value = new ChannelSettingItem()
                {
                    LastUpdateTime = rows[0].LastUpdateTime
                };
                return true;
            }
        }

        public ICollection<ChannelSettingItem> Values
        {
            get
            {
                var result = from row in dataSet.ChannelSettings
                             select new ChannelSettingItem()
                             {
                                 LastUpdateTime = row.LastUpdateTime
                             };
                return result.ToArray();
            }
        }

        public ChannelSettingItem this[int key]
        {
            get
            {
                var result = from row in dataSet.ChannelSettings
                             where row.ChannelHash == key
                             select new ChannelSettingItem()
                             {
                                 LastUpdateTime = row.LastUpdateTime
                             };
                if (result.Count() == 0)
                    throw new InvalidOperationException();
                return result.First();
            }
            set
            {
                var result = (from row in dataSet.ChannelSettings
                              where row.ChannelHash == key
                              select new ChannelSettingItem()
                              {
                                  LastUpdateTime = row.LastUpdateTime
                              }).ToArray();
                if (result.Length == 0)
                {
                    Add(key, value);
                }
                else
                {
                    for (int i = 0; i < result.Length; i++)
                        result[i].LastUpdateTime = value.LastUpdateTime;
                }
                dataSet.ChannelSettings.AcceptChanges();
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,string>> 成员

        public void Add(KeyValuePair<int, ChannelSettingItem> item)
        {
            Add(item.Key, item.Value);
        }

        public void Clear()
        {

            dataSet.ChannelSettings.Clear();
        }

        public bool Contains(KeyValuePair<int, ChannelSettingItem> item)
        {
            return (ContainsKey(item.Key) && this[item.Key].Equals(item.Value));
        }

        public void CopyTo(KeyValuePair<int, ChannelSettingItem>[] array, int arrayIndex)
        {
            int index = arrayIndex;
            foreach (KeyValuePair<int, ChannelSettingItem> pair in this)
            {
                array[arrayIndex + index] = pair;
                index++;
            }
        }

        public int Count
        {
            get
            {
                return dataSet.ChannelSettings.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<int, ChannelSettingItem> item)
        {
            return Remove(item.Key);
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,string>> 成员

        public IEnumerator<KeyValuePair<int, ChannelSettingItem>> GetEnumerator()
        {
            var rows = from row in dataSet.ChannelSettings
                       select row;
            foreach (var row in rows)
            {
                yield return (new KeyValuePair<int, ChannelSettingItem>(row.ChannelHash, new ChannelSettingItem() { LastUpdateTime = row.LastUpdateTime }));
            }
            yield break;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<KeyValuePair<int, ChannelSettingItem>>)this).GetEnumerator() as System.Collections.IEnumerator;
        }

        #endregion
    }
}
